const express = require('express');
const operation = require('../controller/operation')
const router = express.Router();

// 获取公告数据
router.get('/getInfo',(req,res)=>{
    let sql = `SELECT * FROM notice `
       operation.execute(sql,null).then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'获取公告信息成功'
        })
       },err=>{
        res.json({
            status:400,
            msg:'获取公告信息失败'
        })
       })
})
// 添加公告
router.post('/add',(req,res)=>{
    let data ={content,title} = req.body;
    console.log(data);
    let arr =[data.title,data.content]
    let sql = `insert into notice(title,content) values(?,?)`;
       operation.execute(sql,arr).then(result=>{
        res.json({
            status:201,
            data:result,
            msg:'公告添加成功'
        })
       },err=>{
        res.json({
            status:400,
            msg:'公告添加失败'
        })
       })
})
// 删除公告
router.delete('/remove',(req,res)=>{
    let nId = req.query.nId;
    operation.remove(nId,'nId','notice').then(ok=>{
        res.json({
            status:200,
            msg:'删除成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'删除失败'
        })
    })
})
// 公告查询
router.get('/search',(req,res)=>{
    let {query} = req.query;
    let sql  =  `SELECT * FROM (SELECT * FROM comment,stadium,user  WHERE comment.uid = user.uid and comment.sid = stadium.sid ) where comId=${query}`

    operation.execute(sql,null).then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'查询成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'查询失败'
        })
    })
})
// 编辑公告
router.put('/edit',(req,res)=>{
    let data={nId,title,content} = req.body;
    arr=[data.title,data.content,data.nId]
    let sql = `UPDATE notice set title=?,content=? where nId=?`
       operation.execute(sql,arr).then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'更新公告信息成功'
        })
       },err=>{
        res.json({
            status:400,
            msg:'更新公告信息失败'
        })
       })
 
})
module.exports = router;